package tms.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jbundle.util.jcalendarbutton.JCalendarButton;

import tms.Helper;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class ContactInfoPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextField txtAddress;
    private JTextField txtMobileNumber;
    private JTextField txtEmail;
    private JTextField txtLastName;
    private JTextField txtFirstName;
    private JCalendarButton dob;

    private JComboBox cmbTitle;
    private JComboBox cmbCountry;

    public ContactInfoPanel() {
        setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("74dlu"),
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("37dlu"),
                ColumnSpec.decode("37dlu"),
                ColumnSpec.decode("50dlu"),
                ColumnSpec.decode("74dlu"),
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("37dlu"),
                ColumnSpec.decode("37dlu"),
                FormSpecs.GROWING_BUTTON_COLSPEC},
            new RowSpec[] {
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.UNRELATED_GAP_ROWSPEC}));
        add(Tools.createLabel("Title: "), "1, 2, left, fill");
        cmbTitle = Tools.createComboBox();
        cmbTitle.setModel(new DefaultComboBoxModel(new String[] {"Mr", "Mrs", "Miss"}));
        add(cmbTitle, "3, 2, fill, fill");
        add(Tools.createLabel("Country: "), "6, 2, left, fill");
        cmbCountry = Tools.createComboBox();
        cmbCountry.setModel(new DefaultComboBoxModel(new String[] {"Vietnam", "USA", "France"}));
        add(cmbCountry, "8, 2, 2, 1, fill, fill");
        add(Tools.createLabel("First Name: "), "1, 4, left, fill");
        txtFirstName = Tools.createTextField("");
        add(txtFirstName, "3, 4, 2, 1, fill, fill");
        add(Tools.createLabel("Address: "), "6, 4, left, fill");
        txtAddress = Tools.createTextField("");
        add(txtAddress, "8, 4, 2, 1, fill, fill");
        add(Tools.createLabel("Last Name: "), "1, 6, left, fill");
        txtLastName = Tools.createTextField("");
        add(txtLastName, "3, 6, 2, 1, fill, fill");
        add(Tools.createLabel("Mobile Number: "), "6, 6, left, fill");
        txtMobileNumber = Tools.createTextField("");
        add(txtMobileNumber, "8, 6, 2, 1, fill, fill");
        add(Tools.createLabel("Date of birth: "), "1, 8, left, fill");
        dob = new JCalendarButton();
        dob.setFont(Tools.TAHOMA_PLAIN_12);
        dob.setTargetDate(new Date());
        dob.setText(Helper.dateToString(dob.getTargetDate(), Tools.DATE_FORMAT_NO_TIME));
        dob.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date) {
                    dob.setText(Helper.dateToString((Date) evt.getNewValue(), Tools.DATE_FORMAT_NO_TIME));
                }
            }
        });
        add(dob, "3, 8, 2, 1, fill, fill");
        add(Tools.createLabel("Email: "), "6, 8, left, fill");
        txtEmail = Tools.createTextField("");
        add(txtEmail, "8, 8, 2, 1, fill, fill");

        // cmbTitle = Tools.addComboBox(parent, 127, 11, 80, 20, new String[] {
        // "Mr", "Mrs", "Miss" });
        // Tools.addJLabel(parent, "First Name:", null, 10, 45, 75, 14);
        // txtFirstName = Tools.addJTextField(parent, 127, 42, 124, 20, 10);
        // Tools.addJLabel(parent, "Last Name:", null, 10, 76, 75, 14);
        // txtLastName = Tools.addJTextField(parent, 127, 73, 124, 20, 10);
        // Tools.addJLabel(parent, "Day of birth:", null, 10, 107, 75, 14);

        // dob.setBounds(127, 104, 20, 20);
        // parent.add(dob);

        // Tools.addJLabel(parent, "Email:", null, 293, 104, 61, 14);
        // txtEmail = Tools.addJTextField(parent, 398, 104, 111, 20, 10);

        // Tools.addJLabel(parent, "Country:", null, 293, 14, 75, 14);
        // cmbCountry = Tools.addComboBox(parent, 398, 11, 112, 20, new String[]
        // { "Vietnam", "USA", "France" });
        // Tools.addJLabel(parent, "Address:", null, 293, 48, 74, 14);
        // txtAddress = Tools.addJTextField(parent, 398, 45, 111, 20, 10);
        // Tools.addJLabel(parent, "Mobile number:", null, 293, 79, 103, 14);
        // txtMobileNumber = Tools.addJTextField(parent, 398, 76, 112, 20, 10);
        // add(parent);
    }

    public void setTitle(String s) {
        bindingCombobox(cmbTitle, s);
    }

    public void setCountry(String s) {
        bindingCombobox(cmbCountry, s);
    }

    private void bindingCombobox(JComboBox cb, String value) {
        for (int i = 0; i < cb.getItemCount(); i++) {
            if (((String) cb.getItemAt(i)).compareToIgnoreCase(value) == 0) {
                cb.setSelectedIndex(i);
                break;
            }
        }
    }

    public boolean checkValidInput() {

        String fname = txtFirstName.getText();
        String lname = txtLastName.getText();
        String email = txtEmail.getText();
        String phone = txtMobileNumber.getText();
        String address = txtAddress.getText();

        return isValidAccountInfo(fname, lname, email, phone, address)
                && isValidEmailAddress(email);
    }

    private boolean isValidAccountInfo(String fname, String lname,
            String email, String phone, String address) {

        return !fname.trim().isEmpty() && !lname.trim().isEmpty()
                && !email.trim().isEmpty() && !phone.trim().isEmpty()
                && !address.trim().isEmpty();
    }

    public static boolean isValidEmailAddress(String email) {

        boolean result = true;
        try {
            InternetAddress emailAddr = new InternetAddress(email);
            emailAddr.validate();
        } catch (AddressException ex) {
            result = false;
        }
        return result;
    }

    public String getAddress() {
        return txtAddress.getText();
    }

    public void setAddress(String txtAddress) {
        this.txtAddress.setText(txtAddress);
    }

    public String getPhoneNumber() {
        return txtMobileNumber.getText();
    }

    public void setPhoneNumber(String txtMobileNumber) {
        this.txtMobileNumber.setText(txtMobileNumber);
    }

    public String getEmail() {
        return txtEmail.getText();
    }

    public void setEmail(String txtEmail) {
        this.txtEmail.setText(txtEmail);
    }

    public String getLname() {
        return txtLastName.getText();
    }

    public void setLname(String txtLastName) {
        this.txtLastName.setText(txtLastName);
    }

    public String getFname() {
        return txtFirstName.getText();
    }

    public void setFname(String txtFirstName) {
        this.txtFirstName.setText(txtFirstName);
    }

    public String getTitle() {
        return (String) cmbTitle.getSelectedItem();
    }

    public String getCountry() {
        return (String) cmbCountry.getSelectedItem();
    }

    public Date getDOB() {
        return dob.getTargetDate();
    }

    public void setDOB(Date date) {
        this.dob.setTargetDate(date);
    }
}
